var http = require('http')
var path = require('path')
var fs = require('fs')
var express = require('express')
var cookieParser = require('cookie-parser')
var bodyParser = require('body-parser')

var { base64Encode, base64Decode } = require('./lib/base64')
var { getParam } = require('./lib/utils')

/* ------------------------------------------------------------------------------- */
/*
 * api routes
 */

const apiRoutes = express.Router()

// apiRoutes.use(function (req, res, next) {
//   // .. some logic here .. like any other middleware
//   next()
// })

/**
 * extentions for express routers
 */
apiRoutes.use(bodyParser.json())
apiRoutes.use(bodyParser.urlencoded({ extended: false }))
apiRoutes.use(cookieParser())

/**
 * user [login]/[register]/[getUsers]
 */
const user = require('./Routes/user')
apiRoutes.post('/login', user.login)
apiRoutes.post('/logout', user.logout)
apiRoutes.post('/register', user.register)
apiRoutes.all('/showUsers', user.showUsers)

/**
 * api test
 */
apiRoutes.get('/test', (req, res) => res.json({data: 'test success', method: 'get'}))
apiRoutes.post('/test', (req, res) => res.json({data: 'test success', method: 'post'}))
apiRoutes.all('/testid', (req, res) => {
  const { id } = getParam(req, ['id'])
  res.json({id, message: 'get id', type: typeof id})
})
apiRoutes.all('/getMdTest', (req, res) => {
  fs.readFile('./assets/testData.md', 'utf8', (err, data) => {
    if (err) {
      return res.json({code: -1, message: err.message})
    }
    res.json({code: 0, data})
  })
})

apiRoutes.get('/aaa', (req, res) => {
  const { id } = getParam(req, ['id'])
  res.end(id || 'id is null')
})

apiRoutes.get('/img', (req, res) => {
  let imgdata = base64Encode('../assets/CN_tencent.svg')
  let pathName = path.join(__dirname, './a.png')
  // console.log(pathName)
  base64Decode(imgdata, pathName)
  fs.stat(pathName, (exists, data) => {
    res.json({data: imgdata, message: exists})
  })
  // res.json({data: img.src, width: img.width, height: img.height})
})

apiRoutes.get('/base64', (req, res) => {
  let imgdata = base64Encode('./assets/cn_tencent.png')
  res.json({data: imgdata})
})

/**
 * aqi test for baidu echarts
 */
apiRoutes.get('/aqi', (req, res) => {
  console.log('aqi done!')
  http.get('http://echarts.baidu.com/data/asset/data/aqi-beijing.json', response => {
    let data = ''
    response.on('data', d => data += d)
    response.on('end', () => {
      if (data === '') {
        return res.json({message: 'error! no data response'})
      }
      data = JSON.parse(data)
      res.json(data)
    }).on('error', e => {
      console.log(e)
      res.json({message: 'error! get data error'})
    })
  })
})

/**
 * menu list
 */
const { getMenu, updateMenuById, findMenuById, saveMenu, removeMenuById } = require('./Routes/menu')
apiRoutes.all('/getMenu', getMenu)
apiRoutes.all('/updateMenuById', updateMenuById)
apiRoutes.all('/findMenuById', findMenuById)
apiRoutes.all('/saveMenu', saveMenu)
apiRoutes.all('/removeMenuById', removeMenuById)

module.exports = apiRoutes
